package com.yanqu.road.dao.impl.activity.hanghai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hanghai.HangHaiGoods;
import com.yanqu.road.entity.activity.hanghai.HangHaiUserData;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HangHaiUserDataDaoImpl extends TempDataObjectDao<HangHaiUserData> {

    public static String getHangHaiUserDataMapSql = "select * from t_c_activity_hanghai_user_data where activityId=?";

    public Map<Long, HangHaiUserData> getHangHaiUserDataMap(int activityId) {
        Map<Long, HangHaiUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHangHaiUserDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HangHaiUserData hangHaiUserData = parse(rs);
                map.put(hangHaiUserData.getUserId(), hangHaiUserData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateHangHaiUserDataSql = "update t_c_activity_hanghai_user_data set money=?,goodsMap=?,portId=?,toPortId=?,finishTime=?,shipLevel=?,reportRewardList=?,lastResetTime=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(HangHaiUserData hangHaiUserData) {
        hangHaiUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, hangHaiUserData.getMoney().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiUserData.getGoodsMap())));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUserData.getPortId()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUserData.getToPortId()));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiUserData.getFinishTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUserData.getShipLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiUserData.getReportRewardList())));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiUserData.getLastResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiUserData.getUserId()));
        if (getDb().executeNoneQuery(updateHangHaiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHangHaiUserDataSql = "insert into t_c_activity_hanghai_user_data(activityId,userId,money,goodsMap,portId,toPortId,finishTime,shipLevel,reportRewardList,lastResetTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(HangHaiUserData hangHaiUserData) {
        hangHaiUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hangHaiUserData.getMoney().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiUserData.getGoodsMap())));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUserData.getPortId()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUserData.getToPortId()));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiUserData.getFinishTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiUserData.getShipLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiUserData.getReportRewardList())));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiUserData.getLastResetTime()));
        if (getDb().executeNoneQuery(insertHangHaiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected HangHaiUserData parseData(ResultSet rs) throws SQLException {
        HangHaiUserData hangHaiUserData = new HangHaiUserData();
        hangHaiUserData.setActivityId(rs.getInt("activityId"));
        hangHaiUserData.setUserId(rs.getLong("userId"));
        hangHaiUserData.setMoney(new BigDecimal(rs.getString("money")));
        hangHaiUserData.setGoodsMap(JSONObject.parseObject(rs.getString("goodsMap"), new TypeReference<Map<Integer, HangHaiGoods>>() {
        }));
        hangHaiUserData.setPortId(rs.getInt("portId"));
        hangHaiUserData.setToPortId(rs.getInt("toPortId"));
        hangHaiUserData.setFinishTime(rs.getLong("finishTime"));
        hangHaiUserData.setShipLevel(rs.getInt("shipLevel"));
        List<Integer> list = JSONObject.parseObject(rs.getString("reportRewardList"), new TypeReference<List<Integer>>() {
        });
        hangHaiUserData.setReportRewardList(list);
        hangHaiUserData.setLastResetTime(rs.getLong("lastResetTime"));
        return hangHaiUserData;
    }

}
